/*
 * Utilisation de la librairie Ethernet fournie.
 * L'on veut obtenir une adresse IP dynamique à l'aide du protocol DHCP
 * Tant que l'on a pas obtenu cette adresse le soft ne se lance pas
 */

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetDHCP.h>

//TODO BEFORE SELL
//Chaque carte doit avoir une adresse mac différente définie par nos soins
//Pour plus de simplicité on peut considèrer que le n° de série d'une carte est son adresse MAC
byte mac[] = { 0x08, 0xFF, 0xFF, 0x12, 0x12, 0x05 };

const char* ip_to_str(const uint8_t*);

Server server = Server(23);

String readString;

//On déclare la fonction de reset de la carte
void(* resetFunc) (void) = 0;

void setup()
{
  //On définit les E/S ToR en Sorties
  pinMode(A5, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A0, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(2, OUTPUT);
  
  //Serial => Trames débug par liaison série
  Serial.begin(9600);
  Serial.println("Attempting to obtain a DHCP lease...");
  
  //Demande d'IP par le protocol DHCP
  EthernetDHCP.begin(mac);

  //Si l'IP a été obtenue alors on écrit sur la liaison série le résultat
  const byte* ipAddr = EthernetDHCP.ipAddress();
  const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  const byte* dnsAddr = EthernetDHCP.dnsIpAddress();

  //L'adresse IP
  Serial.print("My IP address is ");
  Serial.println(ip_to_str(ipAddr));
  
  //L'IP de la passerelle
  Serial.print("Gateway IP address is ");
  Serial.println(ip_to_str(gatewayAddr));
  
  //L'IP du DNS
  Serial.print("DNS IP address is ");
  Serial.println(ip_to_str(dnsAddr));
  
  //Init server
  Ethernet.begin((uint8_t*)mac, (uint8_t*)ipAddr, (uint8_t*)gatewayAddr, (uint8_t*)dnsAddr);
  server.begin();
}

void loop()
{
  Client client = server.available();
  
  if (client.available()) 
  {
    if( readString.length() > 4 )
      readString = "";
      
    Serial.println("Client disponible");
    char c = client.read();
    
    readString = readString + c;
    
    if( readString == "D" )
    {
      int centaines = (client.read()-48)*100;
      Serial.println(centaines);
      int dizaines = (client.read()-48)*10;
      Serial.println(dizaines);
      int unites = (client.read()-48);
      Serial.println(unites);
      
      centaines = centaines + dizaines + unites;
      
      Serial.println(centaines);
      
      int i = 0;
      
      while(i < centaines)
      {
        int y = 0;
        
        while( y < 60 )
        {
          delay(1000);
          //Serial.println(i);
          y++;
        }
        i++;
      }
      
      readString = "";
    }
    
    //Serial.println(readString);
    
    //Etat haut
    if( readString == "O151")
    {
      Serial.println("Led 15 On");
      digitalWrite(A5, HIGH);
      //EEPROM.write(15, HIGH);
      readString="";
    }
    else if( readString == "O141")
    {
      Serial.println("Led 14 On");
      digitalWrite(A4, HIGH);
      readString="";
    }
    else if( readString == "O131")
    {
      Serial.println("Led 13 On");
      digitalWrite(A3, HIGH);
      readString="";
    }
    else if( readString == "O121")
    {
      Serial.println("Led 12 On");
      digitalWrite(A2, HIGH);
      readString="";
    }
    else if( readString == "O111")
    {
      Serial.println("Led 11 On");
      digitalWrite(A1, HIGH);
      readString="";
    }
    else if( readString == "O101")
    {
      Serial.println("Led 10 On");
      digitalWrite(A0, HIGH);
      readString="";
    }
    else if( readString == "O091")
    {
      Serial.println("Led 9 On");
      digitalWrite(9, HIGH);
      readString="";
    }
    else if(readString == "O081") 
    {
      Serial.println("Led 8 On");
      digitalWrite(8, HIGH);
      readString="";
    }
    else if( readString == "O071")
    {
      Serial.println("Led 7 On");
      digitalWrite(7, HIGH);
      readString="";
    }
    else if( readString == "O061")
    {
      Serial.println("Led 6 On");
      digitalWrite(6, HIGH);
      readString="";
    }
    else if( readString == "O051")
    {
      Serial.println("Led 5 On");
      digitalWrite(5, HIGH);
      readString="";
    }
    else if( readString == "O041")
    {
      Serial.println("Led 4 On");
      digitalWrite(4, HIGH);
      readString="";
    }
    else if( readString == "O031")
    {
      Serial.println("Led 3 On");
      digitalWrite(3, HIGH);
      readString="";
    }
    else if( readString == "O021")
    {
      Serial.println("Led 2 On");
      digitalWrite(2, HIGH);
      readString="";
    }
    
    //Etat Bas
    if( readString == "O150" )
    {
      Serial.println("Led 15 On");
      digitalWrite(A5, LOW);
      readString="";
    }
    else if( readString == "O140" )
    {
      Serial.println("Led 14 On");
      digitalWrite(A4, LOW);
      readString="";
    }
    else if( readString == "O130" )
    {
      Serial.println("Led 13 On");
      digitalWrite(A3, LOW);
      readString="";
    }
    else if( readString == "O120" )
    {
      Serial.println("Led 12 On");
      digitalWrite(A2, LOW);
      readString="";
    }
    else if( readString == "O110" )
    {
      Serial.println("Led 11 On");
      digitalWrite(A1, LOW);
      readString="";
    }
    else if( readString == "O100" )
    {
      Serial.println("Led 10 On");
      digitalWrite(A0, LOW);
      readString="";
    }
    else if( readString == "O090" )
    {
      Serial.println("Led 9 Off");
      digitalWrite(9, LOW);
      readString="";
    }
    else if( readString == "O080" ) 
    {
      Serial.println("Led 8 On");
      digitalWrite(8, LOW);
      readString="";
    }
    else if( readString == "O070" )
    {
      Serial.println("Led 7 On");
      digitalWrite(7, LOW);
      readString="";
    }
    else if( readString == "O060" )
    {
      Serial.println("Led 6 On");
      digitalWrite(6, LOW);
      readString="";
    }
    else if( readString == "O050" )
    {
      Serial.println("Led 5 On");
      digitalWrite(5, LOW);
      readString="";
    }
    else if( readString == "O040" )
    {
      Serial.println("Led 4 On");
      digitalWrite(4, LOW);
      readString="";
    }
    else if( readString == "O030" )
    {
      Serial.println("Led 3 On");
      digitalWrite(3, LOW);
      readString="";
    }
    else if( readString == "O020" )
    {
      Serial.println("Led 2 On");
      digitalWrite(2, LOW);
      readString="";
    }
    
    //Reboot si rebt
    if( readString == "rebt" )
    {
      resetFunc();
    }
   }
  
  //maintain() permet d'obtenir une nouvelle adresse par le DHCP avant quelle n'expire
  EthernetDHCP.maintain();
}

//Formatage d'une adresse IP pour affichage sur liaison série
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}
